package wei.xinya.mynfc;

import android.annotation.SuppressLint;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.nfc.NfcAdapter;
import android.nfc.tech.IsoDep;
import android.nfc.tech.NfcF;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {

    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.textView1)
    TextView textView1;

    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.button1)
    Button button1;

    private NfcAdapter mNfcAdapter;
    private PendingIntent mPendingIntent;

    private String[][] TECHLISTS;
    private IntentFilter[] FILTERS;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        Log.i("MainActivity", "onCreate: ");

        button1.setOnClickListener(v -> {
            textView1.setText("");
            Toast tot = Toast.makeText(MainActivity.this, "清空成功", Toast.LENGTH_LONG);
            tot.show();
        });

        // 初始化nfc
        mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
        Intent intent = new Intent(this, getClass());
        mPendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE);

        if (mNfcAdapter == null) {
            Toast.makeText(this, "NFC不可用", Toast.LENGTH_SHORT).show();
            finish();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i("MainActivity", "onResume");

        try {
            TECHLISTS = new String[][]{{IsoDep.class.getName()}, {NfcF.class.getName()}, {NfcF.class.getName()},};
            FILTERS = new IntentFilter[]{new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED, "*/*")};
        } catch (Exception ignored) {
        }
        if (mNfcAdapter != null) {
            mNfcAdapter.enableForegroundDispatch(this, mPendingIntent, FILTERS, TECHLISTS);
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Log.i("MainActivity", "onNewIntent");

        setIntent(intent);

        if (mNfcAdapter != null) { //有nfc功能
            if (mNfcAdapter.isEnabled()) {//nfc功能打开了
//                resolveIntent(getIntent());
            } else {
                Toast.makeText(this, "请打开NFC功能", Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mNfcAdapter != null) {
            mNfcAdapter.disableForegroundDispatch(this);
        }
    }

    @SuppressLint("SetTextI18n")
    private void resolveIntent(Intent intent) {
        int flags = intent.getFlags();

//        textView1.setText("NFC识别：" + flags);
//        new Thread(() -> NoticeUtil.sendWeiXin(String.valueOf(flags))).start();
    }

}